package com.ap.core.business.message.domain.translators.impl;

import com.ap.core.business.message.domain.translators.BindingListener;
import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.domain.translators.Translator;
import com.ap.core.common.types.MessageStatus;
import com.ap.core.cxf.jaxb.managepayment.ConfirmPayment1Request;
import com.ap.core.cxf.jaxb.paymentdetails.TxnStatusType;
import com.ap.core.cxf.jaxb.rtp.TransactionType;
import com.ap.core.dao.OrderPaymentDao;
import com.ap.core.dao.QuickPayDeliveryAddressDao;
import com.ap.core.domain.AccountNumber;
import com.ap.core.domain.PaymentConfirmationUpdateObj;
import com.ap.core.domain.QuickPayDeliveryAddress;
import com.ap.core.domain.RollNumber;
import com.ap.core.domain.SortCode;
import com.ap.core.domain.sequence.generation.MultiIdGenerationService;
import com.ap.core.exception.BusinessFailureException;

import java.util.EnumSet;

/**
 * User: Rohit.Dixit
 * Implementation class for {@link com.ap.core.business.message.domain.translators.Translator} to translate ConfirmPayment1Request bound object to OrderPayment domain entity.
 */
public class ConfirmPaymentRequestTranslator implements Translator<ConfirmPayment1Request> {

    private final static EnumSet<MessageStatus> processedInputMessages = EnumSet.of(MessageStatus.AUTHORISED, MessageStatus.DECLINED);

    private OrderPaymentDao orderPaymentDao;
    private MultiIdGenerationService<Long> multiIdGenerationService;
    private QuickPayDeliveryAddressDao quickPayDeliveryAddressDao;

    @Override
    public void translate(ConfirmPayment1Request boundObject, BindingMetaData metaData, BindingListener bindingListener) throws BusinessFailureException {
    	// Save QuickPay Address
        final MessageStatus originalMessageStatus = metaData.getOriginalReadOnlyOrderPayment() == null ? null : metaData.getOriginalReadOnlyOrderPayment().getStatusCode();
        boolean isDuplicate = false;
        if(null != originalMessageStatus && processedInputMessages.contains(originalMessageStatus)){
            isDuplicate = true;
        }
        if(null != boundObject.getPaymtDetails().getQkPayDelAddress() && !isDuplicate){
            final QuickPayDeliveryAddress quickPayDeliveryMethod = new QuickPayDeliveryAddress(multiIdGenerationService.getNextId(QuickPayDeliveryAddress.class.getSuperclass()),
                                                                                                                                            metaData.getOriginalOrderPaymentId(),
                                                                                                            boundObject.getPaymtDetails().getQkPayDelAddress().getAddressLine1(),
                                                                                                            boundObject.getPaymtDetails().getQkPayDelAddress().getAddressLine2(),
                                                                                                            boundObject.getPaymtDetails().getQkPayDelAddress().getAddressLine3(),
                                                                                                            boundObject.getPaymtDetails().getQkPayDelAddress().getAddressLine4(),
                                                                                                            boundObject.getPaymtDetails().getQkPayDelAddress().getAddressLine5(),
                                                                                                            boundObject.getPaymtDetails().getQkPayDelAddress().getAddressLine6(),
                                                                                                                boundObject.getPaymtDetails().getQkPayDelAddress().getPostCode(),
                                                                                                             boundObject.getPaymtDetails().getQkPayDelAddress().getCountryCode(),
                                                                                                                     metaData.getReconciliationInfo().getReconciliationCycleId(),
                                                                                                                        metaData.getReconciliationInfo().getReconciliationDate());
            quickPayDeliveryAddressDao.persist(quickPayDeliveryMethod);
        }

        final PaymentConfirmationUpdateObj paymentConfObj = new PaymentConfirmationUpdateObj();
        paymentConfObj.setAccountNumber(AccountNumber.valueOf(boundObject.getConsumerAcct().getAcctNum()));
        paymentConfObj.setFasterPaymentRef(boundObject.getPaymtDetails().getFasterPaymentsRef());
        paymentConfObj.setTransactionStatus(boundObject.getPaymtDetails().getPaymentStatusInfo().getTxnStatus());
        paymentConfObj.setTransactionStatusCode(boundObject.getPaymtDetails().getPaymentStatusInfo().getTxnStatusCode());
        paymentConfObj.setSortCode(SortCode.valueOf(boundObject.getConsumerAcct().getAcctSortCode()));
        paymentConfObj.setPaymentDescTimeStamp(boundObject.getPaymtDetails().getPaymentStatusInfo().getPymtDecTimestamp());
    	if(null != boundObject.getConsumerAcct().getRollNum()){
    		paymentConfObj.setRollNumber(RollNumber.valueOf(boundObject.getConsumerAcct().getRollNum()));
    	}
    	paymentConfObj.setApMessageId(metaData.getApMessageId());
    	
    	MessageStatus messageStatus;
    	if(paymentConfObj.getTransactionStatus() == TxnStatusType.AUTHORISED){
    		messageStatus = MessageStatus.AUTHORISED;
    	}else{
    		messageStatus = MessageStatus.DECLINED;
    	}

        orderPaymentDao.updateOrderPaymentOnConfirmation(metaData.getOriginalOrderPaymentId(),messageStatus, paymentConfObj);
    }

    public void setOrderPaymentDao(OrderPaymentDao orderPaymentDao) {
        this.orderPaymentDao = orderPaymentDao;
    }

    public void setMultiIdGenerationService(MultiIdGenerationService<Long> multiIdGenerationService) {
        this.multiIdGenerationService = multiIdGenerationService;
    }

    public void setQuickPayDeliveryAddressDao(QuickPayDeliveryAddressDao quickPayDeliveryAddressDao) {
        this.quickPayDeliveryAddressDao = quickPayDeliveryAddressDao;
    }
}
